﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Jack.EntityFrameworkCore
{
    public class PagedList<TModel> : IPagedList<TModel>
    {
        private int _currentPage;
        public int CurrentPage => _currentPage;

        private int _pageSize;
        public int PageSize => _pageSize;

        private int _pageCount;
        public int PageCount => _pageCount;

        private int _totalCount;
        public int TotalCount => _totalCount;

        private IEnumerable<TModel> _items;
        public IEnumerable<TModel> Items => _items;

        public PagedList()
        {
        }

        public PagedList(IEnumerable<TModel> items, int currentPage, int pageSize, int totalCount)
        {
            _currentPage = currentPage;
            _pageSize = pageSize;
            _totalCount = totalCount;
            _pageCount = (int)Math.Ceiling((double)totalCount / (double)pageSize);
            _items = items;
        }

        public static PagedList<TModel> Create(IPagedList<TModel> source)
        {
            if (source is PagedList<TModel> pagedList)
            {
                return pagedList;
            }

            return new PagedList<TModel>(source.Items, source.CurrentPage, source.PageSize, source.TotalCount);
        }
    }
}
